package service

import (
	"singo/model"
	"singo/serializer"
)

// ManageSummaryService 工作总结管理列表服务
type ManageSummaryService struct {
	Limit     int    `form:"pageSize" json:"pageSize"`
	Start     int    `form:"pageNo" json:"pageNo"`
	StartTime string `form:"start_time" json:"start_time"`
	EndTime   string `form:"end_time" json:"end_time"`
	ProjectID string `form:"project_id" json:"project_id"`
	Type      string `form:"type" json:"type"`
	Done      bool   `form:"done" json:"done"`
	ExecuteBy string `form:"execute_by" json:"execute_by"`
}

// ManageSummaryList 工作总结管理列表
func (service *ManageSummaryService) ManageSummaryList() serializer.Response {
	manageSummarys := []model.ManageSummary{}
	tasks := []model.Task{}
	demands := []model.Demand{}
	demands2 := []model.Demand{}
	defects := []model.Defect{}
	defects2 := []model.Defect{}
	total := 0
	total2 := 0

	if service.Limit == 0 {
		service.Limit = 10
	}

	if service.Type == "需求" {
		if service.Done {
			if service.StartTime != "" || service.EndTime != "" {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&demands).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&demands).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&demands).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&demands2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&demands).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&demands).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&demands).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&demands2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			} else {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&demands).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Order("status desc, progress desc, complete_at desc").Find(&demands).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&demands).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&demands2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&demands).Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Order("status desc, progress desc, complete_at desc").Find(&demands).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&demands).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&demands2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			}
		} else {
			if service.StartTime != "" || service.EndTime != "" {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&demands).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&demands).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&demands).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&demands2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&demands).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&demands).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&demands).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&demands2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			} else {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&demands).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("status desc, progress desc, deadline_at desc").Find(&demands).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&demands).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&demands2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&demands).Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("status desc, progress desc, deadline_at desc").Find(&demands).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&demands).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where(&model.Demand{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&demands2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			}
		}

		for _, item := range demands {
			manageSummary := model.ManageSummary{
				UID:        item.UID,
				Name:       item.Name,
				Intro:      item.Intro,
				Status:     item.Status,
				Priority:   item.Priority,
				Progress:   item.Progress,
				ProjectID:  item.ProjectID,
				ExecuteBy:  item.ExecuteBy,
				StartAt:    item.StartAt,
				DeadlineAt: item.DeadlineAt,
				CreatedAt:  item.CreatedAt,
				UpdatedAt:  item.UpdatedAt,
				CompleteAt: item.CompleteAt,
			}
			manageSummarys = append(manageSummarys, manageSummary)
		}
		for _, item := range demands2 {
			manageSummary := model.ManageSummary{
				UID:        item.UID,
				Name:       item.Name,
				Intro:      item.Intro,
				Status:     item.Status,
				Priority:   item.Priority,
				Progress:   item.Progress,
				ProjectID:  item.ProjectID,
				ExecuteBy:  item.ExecuteBy,
				StartAt:    item.StartAt,
				DeadlineAt: item.DeadlineAt,
				CreatedAt:  item.CreatedAt,
				UpdatedAt:  item.UpdatedAt,
				CompleteAt: item.CompleteAt,
			}
			manageSummarys = append(manageSummarys, manageSummary)
		}
	} else if service.Type == "缺陷" {
		if service.Done {
			if service.StartTime != "" || service.EndTime != "" {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&defects).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&defects).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&defects).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&defects2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&defects).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&defects).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&defects).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&defects2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			} else {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&defects).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Order("status desc, progress desc, complete_at desc").Find(&defects).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&defects).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&defects2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&defects).Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Order("status desc, progress desc, complete_at desc").Find(&defects).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&defects).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&defects2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			}
		} else {
			if service.StartTime != "" || service.EndTime != "" {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&defects).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&defects).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&defects).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&defects2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&defects).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&defects).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&defects).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&defects2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			} else {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&defects).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("status desc, progress desc, deadline_at desc").Find(&defects).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Model(&defects).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&defects2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&defects).Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("status desc, progress desc, deadline_at desc").Find(&defects).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Model(&defects).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}

					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at IS NULL").Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Defect{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&defects2).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			}
		}

		for _, item := range defects {
			manageSummary := model.ManageSummary{
				UID:        item.UID,
				Name:       item.Name,
				Intro:      item.Intro,
				Status:     item.Status,
				Priority:   item.Priority,
				Progress:   item.Progress,
				ProjectID:  item.ProjectID,
				ExecuteBy:  item.ExecuteBy,
				StartAt:    item.StartAt,
				DeadlineAt: item.DeadlineAt,
				CreatedAt:  item.CreatedAt,
				UpdatedAt:  item.UpdatedAt,
				CompleteAt: item.CompleteAt,
			}
			manageSummarys = append(manageSummarys, manageSummary)
		}

		for _, item := range defects2 {
			manageSummary := model.ManageSummary{
				UID:        item.UID,
				Name:       item.Name,
				Intro:      item.Intro,
				Status:     item.Status,
				Priority:   item.Priority,
				Progress:   item.Progress,
				ProjectID:  item.ProjectID,
				ExecuteBy:  item.ExecuteBy,
				StartAt:    item.StartAt,
				DeadlineAt: item.DeadlineAt,
				CreatedAt:  item.CreatedAt,
				UpdatedAt:  item.UpdatedAt,
				CompleteAt: item.CompleteAt,
			}
			manageSummarys = append(manageSummarys, manageSummary)
		}
	} else {
		if service.Done {
			if service.StartTime != "" || service.EndTime != "" {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&tasks).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Task{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Task{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&tasks).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&tasks).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Task{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("complete_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Task{ProjectID: service.ProjectID, Status: 3}).Order("complete_at desc").Find(&tasks).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			} else {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&tasks).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Task{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Task{ProjectID: service.ProjectID, Status: 3}).Order("status desc, progress desc, complete_at desc").Find(&tasks).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&tasks).Where(&model.Task{ProjectID: service.ProjectID, Status: 3}).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start - 1) * service.Limit).Where(&model.Task{ProjectID: service.ProjectID, Status: 3}).Order("status desc, progress desc, complete_at desc").Find(&tasks).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			}
		} else {
			if service.StartTime != "" || service.EndTime != "" {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&tasks).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Task{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Task{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&tasks).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&tasks).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Task{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("deadline_at BETWEEN ? AND ?", service.StartTime, service.EndTime).Where(&model.Task{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("complete_at desc").Find(&tasks).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			} else {
				if service.ExecuteBy != "" {
					if err := model.DB.Model(&tasks).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Task{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where("execute_by LIKE ?", "%"+service.ExecuteBy+"%").Where(&model.Task{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("status desc, progress desc, deadline_at desc").Find(&tasks).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				} else {
					if err := model.DB.Model(&tasks).Where(&model.Task{ProjectID: service.ProjectID}).Not("status = ?", 3).Count(&total).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
					if err := model.DB.Limit(service.Limit).Offset((service.Start-1)*service.Limit).Where(&model.Task{ProjectID: service.ProjectID}).Not("status = ?", 3).Order("status desc, progress desc, deadline_at desc").Find(&tasks).Error; err != nil {
						return serializer.Response{
							Status: 50000,
							Msg:    "数据库连接错误",
							Error:  err.Error(),
						}
					}
				}
			}
		}

		for _, item := range tasks {
			manageSummary := model.ManageSummary{
				UID:        item.UID,
				Name:       item.Name,
				Intro:      item.Intro,
				Status:     item.Status,
				Priority:   item.Priority,
				Progress:   item.Progress,
				ProjectID:  item.ProjectID,
				ExecuteBy:  item.ExecuteBy,
				Weight:     item.Weight,
				StartAt:    item.StartAt,
				DeadlineAt: item.DeadlineAt,
				CreatedAt:  item.CreatedAt,
				UpdatedAt:  item.UpdatedAt,
				CompleteAt: item.CompleteAt,
			}
			manageSummarys = append(manageSummarys, manageSummary)
		}
	}

	return serializer.BuildListResponse(serializer.BuildSummarysManage(manageSummarys), uint(total+total2))
}
